package com.zipou.framework.gwt.client.widget.suggest;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Widget;
import com.zipou.framework.gwt.client.widget.MyTextBox;

public class MySuggestBox<T extends IsSerializable> extends Widget {

	
	private T value;
	private SuggestBox box;
	private GenericSuggestOracle<T> oracle;

	public MySuggestBox(MyTextBox m) {
		oracle = new GenericSuggestOracle<T>();
		this.box = new SuggestBox(oracle, m);
		this.box.setStyleName("inputEmpty");
		box.addSelectionHandler(new MySelectionHandler<T>(){
			@Override
			public void onSelected(IsSerializable selected) {
				value = (T) selected;
			}
		});
	}

	public final void addSuggestion(T dto) {
		oracle.add(new GenericMultiWordSuggestion<T>(dto));
	}

	public final GenericSuggestOracle<T> getOracle() {
		return this.oracle;
	}

	public final SuggestBox getWidget() {
		return box;
	}
	
	public final T getValue() {
		return value;
	}
	
	public final void setValue(T dto) {
		this.value = dto;
		this.box.setText(dto.toString());
	}
	
	public final void addSelectionHandler(MySelectionHandler<T> handler) {
		box.addSelectionHandler(handler);
	}
}
